package com.qf.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.captcha.LineCaptcha;
import org.junit.Test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.IOException;

/**
 * @author manchi520
 * @date 2022/7/1
 * @apiNote
 */
@Controller
//@RequestMapping("/login")
public class LoginController extends HttpServlet {

    @RequestMapping("vcode")
    public void vCode(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(150, 58);
        lineCaptcha.createCode();
        System.out.println(lineCaptcha.getCode());
        req.getSession().setAttribute("vcode",lineCaptcha.getCode());

        lineCaptcha.write(resp.getOutputStream());
    }


    @RequestMapping("/verify")
    public String login(String email,String password,String vCode,String checkbox, HttpServletRequest req,HttpServletResponse resp){
        String vcode = (String) req.getSession().getAttribute("vcode");
        if (email.equals("2514504811@qq.com")&& password.equals("www")&vCode.equals(vcode)){
            req.getSession().setAttribute("user",email);
            return "forward:/success.jsp";
        }else {
            return "redirect:/sign-in/index.html";
        }
    }

}
